﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;//请先添加引用
namespace TierApplication
{

    public class T_Customer
    {
        public T_Customer()
        { }
        #region Model
        private string _custid;
        private string _custno;
        private string _custtypeid;
        private string _custname;
        private string _custnameen;
        private string _custsource;
        private string _contactname;
        private string _contacttitle;
        private string _contactaddress;
        private string _shipaddress;
        private string _country;
        private string _city;
        private string _region;
        private string _postalcode;
        private string _mobile;
        private string _phone;
        private string _fax;
        private string _qq;
        private string _msn;
        private string _email;
        private string _url;
        private DateTime? _birthday;
        private decimal? _point;
        private DateTime? _creatdate;
        private string _createuser;
        private string _idcardtype;
        private string _idcard;
        private string _userid;
        private string _sex;
        /// <summary>
        /// 
        /// </summary>
        public string CustID
        {
            set { _custid = value; }
            get { return _custid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string CustNo
        {
            set { _custno = value; }
            get { return _custno; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string CustTypeID
        {
            set { _custtypeid = value; }
            get { return _custtypeid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string CustName
        {
            set { _custname = value; }
            get { return _custname; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string CustNameEn
        {
            set { _custnameen = value; }
            get { return _custnameen; }
        }
        /// <summary>
        /// 客户来源
        /// </summary>
        public string CustSource
        {
            set { _custsource = value; }
            get { return _custsource; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ContactName
        {
            set { _contactname = value; }
            get { return _contactname; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ContactTitle
        {
            set { _contacttitle = value; }
            get { return _contacttitle; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ContactAddress
        {
            set { _contactaddress = value; }
            get { return _contactaddress; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ShipAddress
        {
            set { _shipaddress = value; }
            get { return _shipaddress; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Country
        {
            set { _country = value; }
            get { return _country; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string City
        {
            set { _city = value; }
            get { return _city; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Region
        {
            set { _region = value; }
            get { return _region; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string PostalCode
        {
            set { _postalcode = value; }
            get { return _postalcode; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Mobile
        {
            set { _mobile = value; }
            get { return _mobile; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Phone
        {
            set { _phone = value; }
            get { return _phone; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Fax
        {
            set { _fax = value; }
            get { return _fax; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string QQ
        {
            set { _qq = value; }
            get { return _qq; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string MSN
        {
            set { _msn = value; }
            get { return _msn; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Email
        {
            set { _email = value; }
            get { return _email; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string URL
        {
            set { _url = value; }
            get { return _url; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? Birthday
        {
            set { _birthday = value; }
            get { return _birthday; }
        }
        /// <summary>
        /// 
        /// </summary>
        public decimal? Point
        {
            set { _point = value; }
            get { return _point; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? CreatDate
        {
            set { _creatdate = value; }
            get { return _creatdate; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string CreateUser
        {
            set { _createuser = value; }
            get { return _createuser; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string IDCardType
        {
            set { _idcardtype = value; }
            get { return _idcardtype; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string IDCard
        {
            set { _idcard = value; }
            get { return _idcard; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string UserID
        {
            set { _userid = value; }
            get { return _userid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Sex
        {
            set { _sex = value; }
            get { return _sex; }
        }
        #endregion Model


        #region  Method

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public T_Customer(string CustID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append("CustID,CustNo,CustTypeID,CustName,CustNameEn,CustSource,ContactName,ContactTitle,ContactAddress,ShipAddress,Country,City,Region,PostalCode,Mobile,Phone,Fax,QQ,MSN,Email,URL,Birthday,Point,CreatDate,CreateUser,IDCardType,IDCard,UserID,Sex ");
            strSql.Append(" from T_Customer ");
            strSql.Append(" where CustID='" + CustID + "' ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                CustID = ds.Tables[0].Rows[0]["CustID"].ToString();
                CustNo = ds.Tables[0].Rows[0]["CustNo"].ToString();
                CustTypeID = ds.Tables[0].Rows[0]["CustTypeID"].ToString();
                CustName = ds.Tables[0].Rows[0]["CustName"].ToString();
                CustNameEn = ds.Tables[0].Rows[0]["CustNameEn"].ToString();
                CustSource = ds.Tables[0].Rows[0]["CustSource"].ToString();
                ContactName = ds.Tables[0].Rows[0]["ContactName"].ToString();
                ContactTitle = ds.Tables[0].Rows[0]["ContactTitle"].ToString();
                ContactAddress = ds.Tables[0].Rows[0]["ContactAddress"].ToString();
                ShipAddress = ds.Tables[0].Rows[0]["ShipAddress"].ToString();
                Country = ds.Tables[0].Rows[0]["Country"].ToString();
                City = ds.Tables[0].Rows[0]["City"].ToString();
                Region = ds.Tables[0].Rows[0]["Region"].ToString();
                PostalCode = ds.Tables[0].Rows[0]["PostalCode"].ToString();
                Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                QQ = ds.Tables[0].Rows[0]["QQ"].ToString();
                MSN = ds.Tables[0].Rows[0]["MSN"].ToString();
                Email = ds.Tables[0].Rows[0]["Email"].ToString();
                URL = ds.Tables[0].Rows[0]["URL"].ToString();
                if (ds.Tables[0].Rows[0]["Birthday"].ToString() != "")
                {
                    Birthday = DateTime.Parse(ds.Tables[0].Rows[0]["Birthday"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Point"].ToString() != "")
                {
                    Point = decimal.Parse(ds.Tables[0].Rows[0]["Point"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreatDate"].ToString() != "")
                {
                    CreatDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreatDate"].ToString());
                }
                CreateUser = ds.Tables[0].Rows[0]["CreateUser"].ToString();
                IDCardType = ds.Tables[0].Rows[0]["IDCardType"].ToString();
                IDCard = ds.Tables[0].Rows[0]["IDCard"].ToString();
                UserID = ds.Tables[0].Rows[0]["UserID"].ToString();
                Sex = ds.Tables[0].Rows[0]["Sex"].ToString();
            }
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string CustID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Customer where CustID='" + CustID + "' ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [T_Customer](");
            strSql.Append("CustID,CustNo,CustTypeID,CustName,CustNameEn,CustSource,ContactName,ContactTitle,ContactAddress,ShipAddress,Country,City,Region,PostalCode,Mobile,Phone,Fax,QQ,MSN,Email,URL,Birthday,Point,CreatDate,CreateUser,IDCardType,IDCard,UserID,Sex");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + CustID + "',");
            strSql.Append("'" + CustNo + "',");
            strSql.Append("'" + CustTypeID + "',");
            strSql.Append("'" + CustName + "',");
            strSql.Append("'" + CustNameEn + "',");
            strSql.Append("'" + CustSource + "',");
            strSql.Append("'" + ContactName + "',");
            strSql.Append("'" + ContactTitle + "',");
            strSql.Append("'" + ContactAddress + "',");
            strSql.Append("'" + ShipAddress + "',");
            strSql.Append("'" + Country + "',");
            strSql.Append("'" + City + "',");
            strSql.Append("'" + Region + "',");
            strSql.Append("'" + PostalCode + "',");
            strSql.Append("'" + Mobile + "',");
            strSql.Append("'" + Phone + "',");
            strSql.Append("'" + Fax + "',");
            strSql.Append("'" + QQ + "',");
            strSql.Append("'" + MSN + "',");
            strSql.Append("'" + Email + "',");
            strSql.Append("'" + URL + "',");
            strSql.Append("'" + Birthday + "',");
            strSql.Append("" + Point + ",");
            strSql.Append("'" + CreatDate + "',");
            strSql.Append("'" + CreateUser + "',");
            strSql.Append("'" + IDCardType + "',");
            strSql.Append("'" + IDCard + "',");
            strSql.Append("'" + UserID + "',");
            strSql.Append("'" + Sex + "'");
            strSql.Append(")");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Customer set ");
            strSql.Append("CustNo='" + CustNo + "',");
            strSql.Append("CustTypeID='" + CustTypeID + "',");
            strSql.Append("CustName='" + CustName + "',");
            strSql.Append("CustNameEn='" + CustNameEn + "',");
            strSql.Append("CustSource='" + CustSource + "',");
            strSql.Append("ContactName='" + ContactName + "',");
            strSql.Append("ContactTitle='" + ContactTitle + "',");
            strSql.Append("ContactAddress='" + ContactAddress + "',");
            strSql.Append("ShipAddress='" + ShipAddress + "',");
            strSql.Append("Country='" + Country + "',");
            strSql.Append("City='" + City + "',");
            strSql.Append("Region='" + Region + "',");
            strSql.Append("PostalCode='" + PostalCode + "',");
            strSql.Append("Mobile='" + Mobile + "',");
            strSql.Append("Phone='" + Phone + "',");
            strSql.Append("Fax='" + Fax + "',");
            strSql.Append("QQ='" + QQ + "',");
            strSql.Append("MSN='" + MSN + "',");
            strSql.Append("Email='" + Email + "',");
            strSql.Append("URL='" + URL + "',");
            strSql.Append("Birthday='" + Birthday + "',");
            strSql.Append("Point=" + Point + ",");
            strSql.Append("CreatDate='" + CreatDate + "',");
            strSql.Append("CreateUser='" + CreateUser + "',");
            strSql.Append("IDCardType='" + IDCardType + "',");
            strSql.Append("IDCard='" + IDCard + "',");
            strSql.Append("UserID='" + UserID + "',");
            strSql.Append("Sex='" + Sex + "'");
            strSql.Append(" where CustID='" + CustID + "' ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string CustID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Customer ");
            strSql.Append(" where CustID='" + CustID + "' ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(string sCustID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" CustID,CustNo,CustTypeID,CustName,CustNameEn,CustSource,ContactName,ContactTitle,ContactAddress,ShipAddress,Country,City,Region,PostalCode,Mobile,Phone,Fax,QQ,MSN,Email,URL,Birthday,Point,CreatDate,CreateUser,IDCardType,IDCard,UserID,Sex ");
            strSql.Append(" from T_Customer ");
            strSql.Append(" where CustID='" + sCustID + "' ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                CustID = ds.Tables[0].Rows[0]["CustID"].ToString();
                CustNo = ds.Tables[0].Rows[0]["CustNo"].ToString();
                CustTypeID = ds.Tables[0].Rows[0]["CustTypeID"].ToString();
                CustName = ds.Tables[0].Rows[0]["CustName"].ToString();
                CustNameEn = ds.Tables[0].Rows[0]["CustNameEn"].ToString();
                CustSource = ds.Tables[0].Rows[0]["CustSource"].ToString();
                ContactName = ds.Tables[0].Rows[0]["ContactName"].ToString();
                ContactTitle = ds.Tables[0].Rows[0]["ContactTitle"].ToString();
                ContactAddress = ds.Tables[0].Rows[0]["ContactAddress"].ToString();
                ShipAddress = ds.Tables[0].Rows[0]["ShipAddress"].ToString();
                Country = ds.Tables[0].Rows[0]["Country"].ToString();
                City = ds.Tables[0].Rows[0]["City"].ToString();
                Region = ds.Tables[0].Rows[0]["Region"].ToString();
                PostalCode = ds.Tables[0].Rows[0]["PostalCode"].ToString();
                Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                QQ = ds.Tables[0].Rows[0]["QQ"].ToString();
                MSN = ds.Tables[0].Rows[0]["MSN"].ToString();
                Email = ds.Tables[0].Rows[0]["Email"].ToString();
                URL = ds.Tables[0].Rows[0]["URL"].ToString();
                if (ds.Tables[0].Rows[0]["Birthday"].ToString() != "")
                {
                    Birthday = DateTime.Parse(ds.Tables[0].Rows[0]["Birthday"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Point"].ToString() != "")
                {
                    Point = decimal.Parse(ds.Tables[0].Rows[0]["Point"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreatDate"].ToString() != "")
                {
                    CreatDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreatDate"].ToString());
                }
                CreateUser = ds.Tables[0].Rows[0]["CreateUser"].ToString();
                IDCardType = ds.Tables[0].Rows[0]["IDCardType"].ToString();
                IDCard = ds.Tables[0].Rows[0]["IDCard"].ToString();
                UserID = ds.Tables[0].Rows[0]["UserID"].ToString();
                Sex = ds.Tables[0].Rows[0]["Sex"].ToString();
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CustID,CustNo,CustTypeID,CustName,CustNameEn,CustSource,ContactName, ");
            strSql.Append(" ContactTitle,ContactAddress,ShipAddress,Country,City,Region,PostalCode,Mobile,Phone, ");
            strSql.Append(" Fax,QQ,MSN,Email,URL,Birthday,Point,CreatDate,CreateUser,IDCardType,IDCard,UserID, ");
            strSql.Append(" (Select CodeName from c_CustSource where CodeID = a.CustSource) CustSourceName,");
            strSql.Append(" (Select CodeName from c_Sex where CodeID = a.Sex) SexName,");
            strSql.Append(" (Select CodeName from c_CustTypeID where CodeID = a.CustTypeID) CustTypeIDName");
            strSql.Append(" FROM T_Customer a ");
            if (strWhere.Trim() != "")
            {

                strSql.Append(" where 1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}

